//
//  LoginViewController.m
//  XDLBaseProject
//
//  Created by 谢东来 on 2021/8/1.
//  Copyright © 2019 Apple. All rights reserved.
//

#import "LoginViewController.h"
#import "GALCaptcha.h"

@interface LoginViewController ()<UITextFieldDelegate>

@property (strong, nonatomic) UITextField *zhanghaoInput;
@property (strong, nonatomic) UITextField *mimaInput;
@property (strong, nonatomic) UITextField *confirmMimaInput;
@property (strong, nonatomic) UITextField *codeInput;
@property (strong, nonatomic) GALCaptcha * galCaptcha;

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setSubviews];
}

- (void)setSubviews {
//    UIImageView *imV = [[UIImageView alloc] initWithFrame:(CGRectMake(0, 0, kScreenW, kScreenH))];
//    imV.image = [UIImage imageNamed:@"loginbg.png"];
//    imV.contentMode = UIViewContentModeScaleAspectFill;
//    [self.view addSubview:imV];
    
    UILabel *titleLab = [UILabel newAutoLayoutView];
    titleLab.text = @"快速注册";
    titleLab.textColor = [AppColor getColor_MAIN];
    titleLab.font = [UIFont systemFontOfSize:20];
    [self.view addSubview:titleLab];
    [titleLab autoAlignAxisToSuperviewAxis:(ALAxisVertical)];
    [titleLab autoAlignAxis:(ALAxisHorizontal) toSameAxisOfView:self.view withOffset:-kScreenH/2+kTatusBarH+kNavigationBarH/2];
    
    UIButton *backBtn = [UIButton newAutoLayoutView];
//    [backBtn setImage:[UIImage imageNamed:@"nav_fanhui_bai"] forState:(UIControlStateNormal)];
    [backBtn.titleLabel setFont:[UIFont systemFontOfSize:17]];
    [backBtn setTitleColor:[AppColor getColor_MAIN] forState:(UIControlStateNormal)];
    [backBtn setTitle:@"返回" forState:(UIControlStateNormal)];
    [backBtn addTarget:self action:@selector(back) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:backBtn];
    [backBtn autoAlignAxis:(ALAxisHorizontal) toSameAxisOfView:titleLab];
    [backBtn autoPinEdgeToSuperviewEdge:(ALEdgeLeft) withInset:X_Real(5)];
    [backBtn autoSetDimensionsToSize:(CGSizeMake(Width_Real(50), Width_Real(30)))];
    
    
    UIView *zhanghaoV = [[UIView alloc] init];
    zhanghaoV.layer.cornerRadius = Height_Real(25);
    zhanghaoV.layer.borderWidth = Width_Real(1);
    zhanghaoV.layer.borderColor = [AppColor getColor_MAIN].CGColor;
    zhanghaoV.layer.masksToBounds = YES;
    [self.view addSubview:zhanghaoV];
    [zhanghaoV autoSetDimensionsToSize:(CGSizeMake(kScreenW - Width_Real(60), Height_Real(50)))];
    [zhanghaoV autoAlignAxisToSuperviewAxis:(ALAxisVertical)];
    [zhanghaoV autoPinEdgeToSuperviewEdge:(ALEdgeTop) withInset:kTatusBarH + kNavigationBarH + Y_Real(20)];
    UIImageView *zhanghaoImv = [[UIImageView alloc] init];
    zhanghaoImv.backgroundColor = [AppColor getColor_MAIN];
    zhanghaoImv.layer.cornerRadius = Width_Real(15);
    [zhanghaoV addSubview:zhanghaoImv];
    [zhanghaoImv autoAlignAxisToSuperviewAxis:(ALAxisHorizontal)];
    [zhanghaoImv autoPinEdgeToSuperviewEdge:(ALEdgeLeft) withInset:X_Real(10)];
    [zhanghaoImv autoSetDimensionsToSize:(CGSizeMake(Width_Real(30), Width_Real(30)))];
    zhanghaoImv.contentMode = UIViewContentModeScaleAspectFill;
    UIImageView *zhanghaoImv2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"zhanghao.png"]];
    [zhanghaoImv addSubview:zhanghaoImv2];
    [zhanghaoImv2 autoAlignAxisToSuperviewAxis:(ALAxisHorizontal)];
    [zhanghaoImv2 autoAlignAxisToSuperviewAxis:(ALAxisVertical)];
    [zhanghaoImv2 autoSetDimensionsToSize:(CGSizeMake(Width_Real(20), Width_Real(20)))];
    zhanghaoImv2.contentMode = UIViewContentModeScaleAspectFill;
    UITextField *zhanghaoInput = [[UITextField alloc] init];
    zhanghaoInput.delegate = self;
    self.zhanghaoInput = zhanghaoInput;
    zhanghaoInput.inputAccessoryView = [[UIView alloc] initWithFrame:(CGRectMake(0, 0, kScreenW, Height_Real(0.01)))];
    zhanghaoInput.keyboardType = UIKeyboardTypeAlphabet;
    zhanghaoInput.textColor = [UIColor blackColor];
    [zhanghaoInput setFont:[AppFont getFont_14]];
    NSDictionary *dic = @{NSForegroundColorAttributeName:[UIColor lightGrayColor], NSFontAttributeName:[AppFont getFont_14]};
    zhanghaoInput.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入邮箱" attributes:dic];
    [zhanghaoInput addTarget:self action:@selector(textEditingChanged:) forControlEvents:UIControlEventEditingChanged];
    [zhanghaoV addSubview:zhanghaoInput];
    [zhanghaoInput autoAlignAxisToSuperviewAxis:(ALAxisHorizontal)];
    [zhanghaoInput autoPinEdge:(ALEdgeLeft) toEdge:(ALEdgeRight) ofView:zhanghaoImv withOffset:Y_Real(20)];
    [zhanghaoInput autoPinEdgeToSuperviewEdge:(ALEdgeTop) withInset:Y_Real(5)];
    [zhanghaoInput autoPinEdgeToSuperviewEdge:(ALEdgeBottom) withInset:Y_Real(5)];
    [zhanghaoInput autoPinEdgeToSuperviewEdge:(ALEdgeRight) withInset:X_Real(25)];
    
    UIView *mimaV = [[UIView alloc] init];
    mimaV.layer.cornerRadius = Height_Real(25);
    mimaV.layer.borderWidth = Width_Real(1);
    mimaV.layer.borderColor = [AppColor getColor_MAIN].CGColor;
    mimaV.layer.masksToBounds = YES;
    [self.view addSubview:mimaV];
    [mimaV autoSetDimensionsToSize:(CGSizeMake(kScreenW - Width_Real(60), Height_Real(50)))];
    [mimaV autoAlignAxisToSuperviewAxis:(ALAxisVertical)];
    [mimaV autoPinEdge:(ALEdgeTop) toEdge:(ALEdgeBottom) ofView:zhanghaoV withOffset:Y_Real(15)];
    UIImageView *mimaImv = [[UIImageView alloc] init];
    mimaImv.backgroundColor = [AppColor getColor_MAIN];
    [mimaV addSubview:mimaImv];
    mimaImv.layer.cornerRadius = Width_Real(15);
    [mimaImv autoAlignAxisToSuperviewAxis:(ALAxisHorizontal)];
    [mimaImv autoPinEdgeToSuperviewEdge:(ALEdgeLeft) withInset:X_Real(10)];
    [mimaImv autoSetDimensionsToSize:(CGSizeMake(Width_Real(30), Width_Real(30)))];
    mimaImv.contentMode = UIViewContentModeScaleAspectFill;
    UIImageView *mimaImv2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"mima2.png"]];
    [mimaImv addSubview:mimaImv2];
    [mimaImv2 autoAlignAxisToSuperviewAxis:(ALAxisHorizontal)];
    [mimaImv2 autoAlignAxisToSuperviewAxis:(ALAxisVertical)];
    [mimaImv2 autoSetDimensionsToSize:(CGSizeMake(Width_Real(18), Width_Real(18)))];
    mimaImv2.contentMode = UIViewContentModeScaleAspectFill;
    UITextField *mimaInput = [[UITextField alloc] init];
    mimaInput.delegate = self;
    self.mimaInput = mimaInput;
    mimaInput.inputAccessoryView = [[UIView alloc] initWithFrame:(CGRectMake(0, 0, kScreenW, Height_Real(0.01)))];
    mimaInput.keyboardType = UIKeyboardTypeAlphabet;
    mimaInput.returnKeyType = UIReturnKeyDone;
    mimaInput.autocorrectionType = UITextAutocorrectionTypeNo;
    mimaInput.autocapitalizationType = UITextAutocapitalizationTypeNone;
    mimaInput.textColor = [UIColor blackColor];
    [mimaInput setFont:[AppFont getFont_14]];
    dic = @{NSForegroundColorAttributeName:[UIColor lightGrayColor], NSFontAttributeName:[AppFont getFont_14]};
    mimaInput.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请设置密码（8位以上字符）" attributes:dic];
    [mimaInput addTarget:self action:@selector(textEditingChanged:) forControlEvents:UIControlEventEditingChanged];
    [mimaV addSubview:mimaInput];
    [mimaInput autoAlignAxisToSuperviewAxis:(ALAxisHorizontal)];
    [mimaInput autoPinEdge:(ALEdgeLeft) toEdge:(ALEdgeRight) ofView:mimaImv withOffset:Y_Real(20)];
    [mimaInput autoPinEdgeToSuperviewEdge:(ALEdgeTop) withInset:Y_Real(5)];
    [mimaInput autoPinEdgeToSuperviewEdge:(ALEdgeBottom) withInset:Y_Real(5)];
    [mimaInput autoPinEdgeToSuperviewEdge:(ALEdgeRight) withInset:X_Real(20)];
    
    UIView *confirmMimaV = [[UIView alloc] init];
    confirmMimaV.layer.cornerRadius = Height_Real(25);
    confirmMimaV.layer.borderWidth = Width_Real(1);
    confirmMimaV.layer.borderColor = [AppColor getColor_MAIN].CGColor;
    confirmMimaV.layer.masksToBounds = YES;
    [self.view addSubview:confirmMimaV];
    [confirmMimaV autoSetDimensionsToSize:(CGSizeMake(kScreenW - Width_Real(60), Height_Real(50)))];
    [confirmMimaV autoAlignAxisToSuperviewAxis:(ALAxisVertical)];
    [confirmMimaV autoPinEdge:(ALEdgeTop) toEdge:(ALEdgeBottom) ofView:mimaV withOffset:Y_Real(15)];
    UIImageView *confirmMimaImv = [[UIImageView alloc] init];
    confirmMimaImv.backgroundColor = [AppColor getColor_MAIN];
    [confirmMimaV addSubview:confirmMimaImv];
    confirmMimaImv.layer.cornerRadius = Width_Real(15);
    [confirmMimaImv autoAlignAxisToSuperviewAxis:(ALAxisHorizontal)];
    [confirmMimaImv autoPinEdgeToSuperviewEdge:(ALEdgeLeft) withInset:X_Real(10)];
    [confirmMimaImv autoSetDimensionsToSize:(CGSizeMake(Width_Real(30), Width_Real(30)))];
    confirmMimaImv.contentMode = UIViewContentModeScaleAspectFill;
    UIImageView *confirmMimaImv2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"mima.png"]];
    [confirmMimaImv addSubview:confirmMimaImv2];
    [confirmMimaImv2 autoAlignAxisToSuperviewAxis:(ALAxisHorizontal)];
    [confirmMimaImv2 autoAlignAxisToSuperviewAxis:(ALAxisVertical)];
    [confirmMimaImv2 autoSetDimensionsToSize:(CGSizeMake(Width_Real(18), Width_Real(18)))];
    confirmMimaImv.contentMode = UIViewContentModeScaleAspectFill;
    UITextField *confirmMimaInput = [[UITextField alloc] init];
    confirmMimaInput.delegate = self;
    self.confirmMimaInput = confirmMimaInput;
    confirmMimaInput.inputAccessoryView = [[UIView alloc] initWithFrame:(CGRectMake(0, 0, kScreenW, Height_Real(0.01)))];
    confirmMimaInput.keyboardType = UIKeyboardTypeAlphabet;
    confirmMimaInput.returnKeyType = UIReturnKeyDone;
    confirmMimaInput.autocorrectionType = UITextAutocorrectionTypeNo;
    confirmMimaInput.autocapitalizationType = UITextAutocapitalizationTypeNone;
    confirmMimaInput.textColor = [UIColor blackColor];
    [confirmMimaInput setFont:[AppFont getFont_14]];
    dic = @{NSForegroundColorAttributeName:[UIColor lightGrayColor], NSFontAttributeName:[AppFont getFont_14]};
    confirmMimaInput.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请确认密码（8位以上字符）" attributes:dic];
    [confirmMimaInput addTarget:self action:@selector(textEditingChanged:) forControlEvents:UIControlEventEditingChanged];
    [confirmMimaV addSubview:confirmMimaInput];
    [confirmMimaInput autoAlignAxisToSuperviewAxis:(ALAxisHorizontal)];
    [confirmMimaInput autoPinEdge:(ALEdgeLeft) toEdge:(ALEdgeRight) ofView:mimaImv withOffset:Y_Real(20)];
    [confirmMimaInput autoPinEdgeToSuperviewEdge:(ALEdgeTop) withInset:Y_Real(5)];
    [confirmMimaInput autoPinEdgeToSuperviewEdge:(ALEdgeBottom) withInset:Y_Real(5)];
    [confirmMimaInput autoPinEdgeToSuperviewEdge:(ALEdgeRight) withInset:X_Real(20)];
    
    UIView *codeV = [[UIView alloc] init];
    codeV.layer.cornerRadius = Height_Real(25);
    codeV.layer.borderWidth = Width_Real(1);
    codeV.layer.borderColor = [AppColor getColor_MAIN].CGColor;
    codeV.layer.masksToBounds = YES;
    [self.view addSubview:codeV];
    [codeV autoSetDimensionsToSize:(CGSizeMake(kScreenW - Width_Real(60), Height_Real(50)))];
    [codeV autoAlignAxisToSuperviewAxis:(ALAxisVertical)];
    [codeV autoPinEdge:(ALEdgeTop) toEdge:(ALEdgeBottom) ofView:confirmMimaV withOffset:Y_Real(15)];
    UIImageView *codeImv = [[UIImageView alloc] init];
    codeImv.backgroundColor = [AppColor getColor_MAIN];
    codeImv.layer.cornerRadius = Width_Real(15);
    [codeV addSubview:codeImv];
    [codeImv autoAlignAxisToSuperviewAxis:(ALAxisHorizontal)];
    [codeImv autoPinEdgeToSuperviewEdge:(ALEdgeLeft) withInset:X_Real(10)];
    [codeImv autoSetDimensionsToSize:(CGSizeMake(Width_Real(30), Width_Real(30)))];
    codeImv.contentMode = UIViewContentModeScaleAspectFill;
    UIImageView *codeImv2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"phone.png"]];
    [codeImv addSubview:codeImv2];
    [codeImv2 autoAlignAxisToSuperviewAxis:(ALAxisHorizontal)];
    [codeImv2 autoAlignAxisToSuperviewAxis:(ALAxisVertical)];
    [codeImv2 autoSetDimensionsToSize:(CGSizeMake(Width_Real(15), Width_Real(15)))];
    codeImv2.contentMode = UIViewContentModeScaleAspectFill;
    UITextField *codeInput = [[UITextField alloc] init];
//    codeInput.backgroundColor = randomColor;
    codeInput.delegate = self;
    self.codeInput = codeInput;
    codeInput.inputAccessoryView = [[UIView alloc] initWithFrame:(CGRectMake(0, 0, kScreenW, Height_Real(0.01)))];
    codeInput.keyboardType = UIKeyboardTypeAlphabet;
    codeInput.returnKeyType = UIReturnKeyDone;
    codeInput.autocorrectionType = UITextAutocorrectionTypeNo;
    codeInput.autocapitalizationType = UITextAutocapitalizationTypeNone;
    codeInput.textColor = [UIColor blackColor];
    [codeInput setFont:[AppFont getFont_14]];
    dic = @{NSForegroundColorAttributeName:[UIColor lightGrayColor], NSFontAttributeName:[AppFont getFont_14]};
    codeInput.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入验证码" attributes:dic];
    [codeInput addTarget:self action:@selector(textEditingChanged:) forControlEvents:UIControlEventEditingChanged];
    [codeV addSubview:codeInput];
    [codeInput autoAlignAxisToSuperviewAxis:(ALAxisHorizontal)];
    [codeInput autoPinEdge:(ALEdgeLeft) toEdge:(ALEdgeRight) ofView:mimaImv withOffset:Y_Real(20)];
    [codeInput autoPinEdgeToSuperviewEdge:(ALEdgeTop) withInset:Y_Real(5)];
    [codeInput autoPinEdgeToSuperviewEdge:(ALEdgeBottom) withInset:Y_Real(5)];
    [codeInput autoPinEdgeToSuperviewEdge:(ALEdgeRight) withInset:Y_Real(105)];
    //
    self.galCaptcha = [[GALCaptcha alloc] initWithFrame:(CGRectMake(kScreenW - Width_Real(60)-Width_Real(105), 0, Width_Real(80), Height_Real(40)))];
    self.galCaptcha.centerY = Y_Real(25);
    [codeV addSubview:self.galCaptcha];
    //
//    UIButton *codeBtn = [UIButton newAutoLayoutView];
//    [codeBtn setTitle:@"獲取驗證碼" forState:(UIControlStateNormal)];
//    [codeBtn setTitleColor:UIColorFromHex(0xc00000) forState:(UIControlStateNormal)];
//    codeBtn.titleLabel.font = [AppFont getFont_13];
//    [codeBtn addTarget:self action:@selector(sendSmsAuthCode:) forControlEvents:(UIControlEventTouchUpInside)];
//    [codeV addSubview:codeBtn];
//    [codeBtn autoSetDimensionsToSize:(CGSizeMake(Width_Real(Width_Real(80)), Height_Real(40)))];
//    [codeBtn autoPinEdgeToSuperviewEdge:(ALEdgeRight) withInset:X_Real(10)];
//    [codeBtn autoPinEdge:(ALEdgeLeft) toEdge:(ALEdgeRight) ofView:codeInput withOffset:X_Real(15)];
//    [codeBtn autoAlignAxisToSuperviewAxis:(ALAxisHorizontal)];
    
    UIButton *loginBtn = [[UIButton alloc] init];
    [loginBtn setBackgroundImage:[UIImage imageWithColor:[AppColor getColor_MAIN] size:CGSizeMake(kScreenW - Width_Real(60), Height_Real(50))] forState:(UIControlStateNormal)];
    [loginBtn setTitle:@"注    册" forState:(UIControlStateNormal)];
    [loginBtn setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    loginBtn.titleLabel.font = [AppFont getFont_19];
    loginBtn.layer.cornerRadius = Height_Real(25);
    loginBtn.layer.masksToBounds = YES;
    [loginBtn addTarget:self action:@selector(clickLogin) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:loginBtn];
    [loginBtn autoSetDimensionsToSize:(CGSizeMake(kScreenW - Width_Real(60), Height_Real(50)))];
    [loginBtn autoAlignAxisToSuperviewAxis:(ALAxisVertical)];
    [loginBtn autoPinEdge:(ALEdgeTop) toEdge:(ALEdgeBottom) ofView:codeV withOffset:Y_Real(80)];
}


- (void)clickLogin {
    [self.view endEditing:YES];
    if (self.mimaInput.text.length < 8){
        [SVProgressHUD showErrorWithStatus:@"密码最少应为8位"];
    } else if (![self.mimaInput.text isEqualToString:self.confirmMimaInput.text]) {
        [SVProgressHUD showErrorWithStatus:@"确认密码必须与密码一致"];
    } else if (self.codeInput.text.length < 4) {
        [SVProgressHUD showErrorWithStatus:@"验证码必须为4位"];
    } else if ([self.codeInput.text compare:self.galCaptcha.CatString options:NSCaseInsensitiveSearch]) {
        [SVProgressHUD showErrorWithStatus:@"验证码错误"];
    } else {
        [SVProgressHUD show];
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        NSDictionary *parameters = @{@"email":self.zhanghaoInput.text,
                                     @"password":self.mimaInput.text
                                     };
        [manager DL_POST:API_REGISTER parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
            NSLog(@"%@", responseObject);
            if ([responseObject[@"code"] integerValue] == 1) {
                [SVProgressHUD showSuccessWithStatus:@"注册成功"];
            } else {
                [SVProgressHUD showErrorWithStatus:responseObject[@"info"]];
            }
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            NSLog(@"%@", error);
            [SVProgressHUD showErrorWithStatus:@"注册失敗"];
        }];
    }
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [self.view endEditing:YES];
    return YES;
}

//- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
//    if (textField == self.mimaInput || textField == self.confirmMimaInput || textField == self.codeInput) {
//        NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:ALPHANUM] invertedSet];
//        NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
//        return [string isEqualToString:filtered];
//    } else if (textField == self.zhanghaoInput) {
//        NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:NUM] invertedSet];
//        NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
//        return [string isEqualToString:filtered];
//    } else {
//        return NO;
//    }
//}

- (void)textEditingChanged:(UITextField *)textField {
    if (textField == self.zhanghaoInput || textField == self.mimaInput || textField == self.confirmMimaInput) {
        if ([textField.text length] > 25) {
            textField.text = [textField.text substringToIndex:25];
        }
    } else if (textField == self.codeInput) {
        if ([textField.text length] > 4) {
            textField.text = [textField.text substringToIndex:4];
        }
    }
}

- (void)back {
    [self.navigationController popViewControllerAnimated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
